﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace N03_11HCA_HTTTHD.Controllers
{
    public class HomeController : Controller
    {

        Data_DAO_DTO_Lib.TINTUC_DAO tinTucDAO = new Data_DAO_DTO_Lib.TINTUC_DAO();
        Data_DAO_DTO_Lib.THONGTINCANBAN_DAO canBanDAO = new Data_DAO_DTO_Lib.THONGTINCANBAN_DAO();
        Data_DAO_DTO_Lib.THONGTINTIMMUA_DAO timMuaDAO = new Data_DAO_DTO_Lib.THONGTINTIMMUA_DAO();
        Data_DAO_DTO_Lib.RAOVAT_DAO raoVatDAO = new Data_DAO_DTO_Lib.RAOVAT_DAO();
        //
        // GET: /Home/
        public ActionResult Index()
        {
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            N03_11HCA_HTTTHD.Models.Home model = new Models.Home(tinTucDAO.LayDanhSachCoThamSo(0, 5, "NgayDang DESC"), canBanDAO.LayDanhSachCoThamSo(0, 5, "NgayDang DESC"), timMuaDAO.LayDanhSachCoThamSo(0, 5, "NgayDang DESC"), raoVatDAO.LayDanhSachCoThamSo(0, 5, "ThoiGianRao DESC"));
            return View("Home",model);
        }

        Data_DAO_DTO_Lib.DANHMUC_DAO danhMucDAO = new Data_DAO_DTO_Lib.DANHMUC_DAO();

        [ChildActionOnly]
        public ActionResult MenuTraiCanBan()
        {
            Models.MenuTrai model = new Models.MenuTrai(danhMucDAO.LayDanhSachCoCanBan());
            return PartialView("MenuTraiCanBan", model);
        }
        [ChildActionOnly]
        public ActionResult MenuTraiTimMua()
        {
            Models.MenuTrai model = new Models.MenuTrai(danhMucDAO.LayDanhSachCoTimMua());
            return PartialView("MenuTraiTimMua", model);
        }
        
        //
        // GET: /Home/DanhMucCanBan/1
        public ActionResult DanhMucCanBan(int id_TimMua)
        {
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            Models.DanhMucCanBan modal = new Models.DanhMucCanBan(id_TimMua);
            return View("DanhMucCanBan", modal);
        }

        //
        // GET: /Home/DanhMucCanBan?
        [HttpPost]
        public JsonResult DanhMucCanBan(int id, int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            return new JsonResult() { Data = danhMucDAO.LayDanhSachCanBanTrongDanhMuc(id, jtStartIndex, jtPageSize, jtSorting )};
        }



        //
        // GET: /Home/DanhMucTimMua/
        public ActionResult DanhMucTimMua(int id_TimMua)
        {
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            Models.DanhMucCanBan modal = new Models.DanhMucCanBan(id_TimMua);
            return View("DanhMucTimMua", modal);
        }

        //
        // GET: /Home/DanhMucTimMua/
        [HttpPost]
        public JsonResult DanhMucTimMua(int id, int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {

            return new JsonResult() { Data = danhMucDAO.LayDanhSachTimMuaTrongDanhMuc(id, jtStartIndex, jtPageSize, jtSorting) };
        }

        //ThongTinTaiKhoan
        /*
        public ActionResult ThongTin()
        {
            if (Session["ID"] == null)
            {
                return RedirectToAction("DangNhap");
            }
            
            return View("ThongTinTaiKhoan", tkM);
        }
        */

        public ActionResult PhucHoiMatKhau()
        {
            if (Session["ID"] != null)
            {
                return RedirectToAction("Index", "Home");
            }
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            return View("PhucHoiMatKhau");
        }
        
        public ActionResult DangNhap()
        {

            if (Session["ID"] != null)
            {
                return RedirectToAction("Index", "Home");
            }
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            N03_11HCA_HTTTHD.Models.DangNhap model = new Models.DangNhap("", "");
            return View("DangNhap", model);
        }

        public ActionResult DangXuat()
        {
            
            Session["ID"] = null;
            Session["TenDangNhap"] = null;
            Session["IDLoaiTK"] = null;
            Session["IDLoaiTV"] = null;

            return RedirectToAction("Index", "Home");
        }

        Data_DAO_DTO_Lib.TAIKHOAN_DAO taiKhoanDAO = new Data_DAO_DTO_Lib.TAIKHOAN_DAO();
        [HttpPost]
        public ActionResult DangNhap(FormCollection form)
        {
            
            String username = form["username"];
            String pass = form["pass"];
            try
            {
                if (Session["ID"] != null)
                {
                    throw new Exception("Hiện tại đang có tài khoản đăng nhập, vui lòng thoát.");
                }
    
                Data_DAO_DTO_Lib.DTO.TAIKHOAN_DTO tk = taiKhoanDAO.LayTuTenDanhNhap_MatKhau(username, pass);

                Session["ID"] = tk.ID;
                Session["TenDangNhap"] = tk.TenDangNhap;
                Session["IDLoaiTK"] = tk.IDLoaiTK;
                Session["IDLoaiTV"] = tk.IDLoaiTV;
            }
            catch (Exception ex)
            {
                N03_11HCA_HTTTHD.Models.DangNhap model = new Models.DangNhap(username,ex.Message);

                return View("DangNhap", model);
            }

            return RedirectToAction("Index", "Home");
        }

        public ActionResult LienHe(int idNguoiCanLienHe, String ChuDeLienHe)
        {
            if (N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session).ID1 == null)
            {
                return RedirectToAction("Index", "Home");
            }

            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);

            Data_DAO_DTO_Lib.TAIKHOAN_DAO tkDAO = new Data_DAO_DTO_Lib.TAIKHOAN_DAO();

            String ten = tkDAO.LayTenNguoiNhanLienHeTuID(idNguoiCanLienHe);

            Models.LienHe model = new Models.LienHe(idNguoiCanLienHe, ten, ChuDeLienHe, "", "", "");
            return View("LienHe",model);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult LienHe(FormCollection mangDATA)
        {
            if (N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session).ID1 == null)
            {
                return RedirectToAction("Index", "Home");
            }

            Models.LienHe model = null;
            String ChuDe = "";
            String EmailNguoiGoi = "";
            int NguoihNan = 0;
            String NoiDung = "";
            String tenNguoiNhan = "";
            try
            {
                NguoihNan = Int32.Parse(mangDATA["IDNguoiNhan"]);
                NoiDung = mangDATA["NoiDung"];
                ChuDe = mangDATA["ChuDe"];
                EmailNguoiGoi = mangDATA["Email"];
                tenNguoiNhan =  mangDATA["TenNguoiNhan"];

                Data_DAO_DTO_Lib.THONGDIEP_DAO tdDAO = new Data_DAO_DTO_Lib.THONGDIEP_DAO();
                Data_DAO_DTO_Lib.DTO.THONGDIEP_DTO td = new Data_DAO_DTO_Lib.DTO.THONGDIEP_DTO();
                td.NguoiGoi = (int)N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session).ID1;
                
                td.NguoihNan = NguoihNan;
                td.ChuDe = ChuDe;
                td.EmailNguoiGoi = EmailNguoiGoi;
                td.NoiDung = NoiDung;
                td.ThoiGianGoi = DateTime.Now;
                td.Xoa = false;

                tdDAO.Them(td);
                model = new Models.LienHe(NguoihNan, tenNguoiNhan, ChuDe, NoiDung, EmailNguoiGoi, "Liên hệ thành công!");
            }
            catch (Exception e)
            {
                model = new Models.LienHe(NguoihNan, tenNguoiNhan, ChuDe, NoiDung, EmailNguoiGoi, e.Message);
                
            }
            ViewBag.TK = N03_11HCA_HTTTHD.TienIch.HeThong.LayThongTinTKHienTai(Session);
            return View("LienHe", model);
        }
        /*
        [HttpPost]
        public ActionResult Register(FormCollection form)
        {
            try
            {
                String username = form["username"];
                String pass = form["pass"];
                String pass2 = form["pass2"];
                String email = form["email"];
                String diachi = form["diachi"];
                String dienthoai = form["dienthoai"];
                String gioitinh = form["gioitinh"];
                String hoten = form["hoten"];

                DAO.TAIKHOAN tk = new DAO.TAIKHOAN();
                tk.DiaChi = diachi;
                tk.DienThoai = dienthoai;
                tk.Email = email;
                tk.GioiTinh = gioitinh;
                tk.HoTen = hoten;
                tk.MatKhau = pass;
                tk.TenDangNhap = username;
                //Mua
                tk.LoaiTaiKhoan = 1;
                tk.Xoa = false;
                tkDAO.Insert(tk);

                return ThongBao("Chúc mừng, bạn đã đăng ký thành công tài khoản " + username + ". Hãy tiến hành đăng nhập ngay để có thể nhanh chóng tham gia đấu giá.");
            }
            catch (Exception ex)
            {
                ViewBag.Loi = ex.Message;
                return View("DangKy");
            }
        }

        [HttpPost]
        public String DoiMatKhau(FormCollection form)
        {
            if (Session["ID"] == null)
            {
                return "Hãy đăng nhập để cập nhật thông tin";
            }
            else if ((Boolean)Session["isNhanVien"] == true)
            {
                return "Hãy đăng nhập để cập nhật thông tin";
            }

            try
            {
                String pass1 = form["pass1"];
                String pass2 = form["pass2"];
                String pass3 = form["pass3"];
                if (pass3 != pass2)
                {
                    return "Mật khẩu mới không giống nhau";
                }
                tkDAO.DoiMatKhau((int)Session["ID"], pass1, pass2);

                return "Chúc mừng, bạn đã cập nhật mật khẩu thành công ";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        */

    }
}

